package phantm.phases

import phantm.Settings
import phantm.util.{ API, Reporter }
import java.io.File

object APIImportationPhase extends Phase {
  def name = "API importation"
  def description = "Importing API from XML files"

  def run(ctx: PhasesContext): PhasesContext = {
    if (Settings.get.importAPI) {
      
      val pathToBin = getClass().getClassLoader().getResource("").getPath()
      val path = pathToBin.substring(0, pathToBin.indexOf("bin"))
      var internal_api = new java.io.FileInputStream(path + "spec/internal_api.xml")

      new API.Reader(internal_api, ctx).load

      for (api <- Settings.get.apis) {
        if (new File(api).exists()) {
          new API.Reader(api, ctx).load
        } else {
          Reporter.get.error("Failed to load API file '" + api + "': File not found");
        }
      }
    }
    ctx
  }
}
